/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package evidence.model.pomocneTridy;

import java.io.Serializable;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Protheo
 */
public class Time implements Serializable, Comparable<Time>, ITime {

    public static final long serialVersionUID = 1;
    private final Integer hodiny;
    private final Integer minuty;

    public Time(Integer hodiny, Integer minuty) throws TimeException {
        if (hodiny < 0 || hodiny > 23 || minuty < 0 || minuty > 59) {
            throw new TimeException("ŠPATĚ ZADANÝ ČAS");
        }
        this.hodiny = new Integer(hodiny);
        this.minuty = new Integer(minuty);
    }

    @SuppressWarnings("AccessingNonPublicFieldOfAnotherObject")
    public Time(Time cas) {
        this.hodiny = new Integer(cas.hodiny);
        this.minuty = new Integer(cas.minuty);
    }

    public Time(String cas) throws TimeException {
        String[] pom = cas.split(":");
        if (pom.length != 2) {
            throw new TimeException("Neplatný formát času");
        }
        int hod = 0, min = 0;
        try {
            hod = Integer.parseInt(pom[0]);
            min = Integer.parseInt(pom[1]);
        } catch (NumberFormatException ex) {
            throw new TimeException("Neplatný formát času");
        }
        this.hodiny = hod;
        this.minuty = min;
    }

    public int getHodiny() {
        return hodiny;
    }

    public int getMinuty() {
        return minuty;
    }

    @Override
    @SuppressWarnings("AccessingNonPublicFieldOfAnotherObject")
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Time other = (Time) obj;
        if (this.hodiny != other.hodiny && (this.hodiny == null || !this.hodiny.equals(other.hodiny))) {
            return false;
        }
        if (this.minuty != other.minuty && (this.minuty == null || !this.minuty.equals(other.minuty))) {
            return false;
        }
        return true;
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 67 * hash + (this.hodiny != null ? this.hodiny.hashCode() : 0);
        hash = 67 * hash + (this.minuty != null ? this.minuty.hashCode() : 0);
        return hash;
    }

    @Override
    public String toString() {
        String pom = minuty.toString();
        if (minuty == 0) {
            pom = "00";
        }
        return hodiny + ":" + pom;
    }

    public double odectiCas(Time cas) {
        if (this.minutOdPulnoci() < cas.minutOdPulnoci()) {
            return ((double) this.minutOdPulnoci() + 1440 - cas.minutOdPulnoci()) / 60;
        } else {
            return ((double) this.minutOdPulnoci() - cas.minutOdPulnoci()) / 60;
        }
    }

    private int minutOdPulnoci() {
        return this.minuty + this.hodiny * 60;
    }

    public int compareTo(Time cas) {
        if (this.minutOdPulnoci() > cas.minutOdPulnoci()) {
            return 1;
        } else {
            if (this.minutOdPulnoci() == cas.minutOdPulnoci()) {
                return 0;
            } else {
                return -1;
            }
        }
    }

    public Time prictiCas(int hodiny, int minuty) {
        int pom = this.minutOdPulnoci() + 60 * hodiny + minuty;
        int hod = pom / 60;
        if (hod > 23) {
            hod %= 24;
        }
        int min = pom % 60;
        Time cas = null;
        try {
            cas = new Time(hod, min);
        } catch (TimeException ex) {
            Logger.getLogger(Time.class.getName()).log(Level.SEVERE, null, ex);
        }
        return cas;
    }
}
